//
//  HMComposeBottomView.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
enum HMComposeBottomViewType: Int {
    //图片
    case Picture = 0
    // @
    case Mention = 1
    // #
    case Trend = 2
    // 笑脸
    case Emoticon = 3
    // +
    case Add = 4
}

class HMComposeBottomView: UIView {
    
    var emoticonButton: UIButton?
    
    //声明一个闭包  01
    var closure:((type:HMComposeBottomViewType) -> ())?
    //定义一个属性 供外界设置
    var isEnoticon: Bool = false{
        didSet{
            /**
             - true   显示表情键盘
             - false  反之系统键盘
             */
            let imageName: String
            if isEnoticon{
            //显示表情键盘
            imageName = "compose_keyboardbutton_background"
            
            }else{
            //系统键盘
            imageName = "compose_emoticonbutton_background"
            }
            //设置图片
            emoticonButton?.setImage(UIImage(named:imageName), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named:"\(imageName)_Highlighted"), forState: UIControlState.Highlighted)
        
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //监听事件
    func buttonClick(button:UIButton){
   
    //执行闭包 03
        closure?(type: HMComposeBottomViewType(rawValue: button.tag)! )
    
    }
    
     // MARK: - 设置UI
    private func setupUI(){
        //带你飞方法
    backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        
        //增加按钮
        let pictureButton = addChildBtns("compose_toolbar_picture",type:.Picture)
        let mentionButton = addChildBtns("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildBtns("compose_trendbutton_background",type: .Trend)
        emoticonButton = addChildBtns("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = addChildBtns("compose_add_background",type: .Add)
        //添加约束
        pictureButton.snp_makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        emoticonButton!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        addButton.snp_makeConstraints { (make) in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
        
        
    }
    func addChildBtns(imageName:String, type:HMComposeBottomViewType) -> UIButton{
        let btn = UIButton()
        // 设置tag //type.rawValue 通过枚举名 得到对应的枚举值
        btn.tag = type.rawValue
        //增加事件
        
        //注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！注意冒号！！！
        btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setImage(UIImage(named:imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"\(imageName)_Highlighted"), forState: UIControlState.Highlighted)
        addSubview(btn)
        return btn
    }
}
